package dao

import (
	"github.com/crack007/go-im/src/common/entity"
	"github.com/crack007/go-im/src/common/util"
	"gorm.io/gorm"
)

type noticeDao struct {
	db *gorm.DB
}

func (d *noticeDao) Save(noticeEntity *entity.NoticeEntity) {
	if noticeEntity.ID > 0 {
		noticeEntity.UpdatedAt = util.GetDatetime()
	}
	d.db.Save(noticeEntity)
}

func (d *noticeDao) List(userId uint32, page uint32, size uint32) []*entity.NoticeEntity {
	var list []*entity.NoticeEntity
	offset := (page - 1) * size
	d.db.Where("is_delete=0 and user_id=?", userId).Limit(int(size)).Offset(int(offset)).Find(&list)
	return list
}

func (d *noticeDao) GetById(id uint32) *entity.NoticeEntity {
	var e *entity.NoticeEntity
	d.db.Where("id=?", id).Find(&e)
	if e.ID == 0 || e.IsDelete {
		return nil
	}
	return e
}
